with Legend.Stages;
package body Legend.Roles.Specials is

   function Try_Enter_Scene (
      this : in out Player_Type;
      target : Point_Type
      ) return Boolean
   is
      scene : Uint16_t;
   begin
      if not this.F_Stage.Find_Scene_At(target, scene) then
         return False;
      end if;
      if not Can_Enter(All_World.Scenes(scene)) then
         return False;
      end if;
      this.Stop_Walk;
      this.F_Stage.Switch_To_Room(All_World.Scenes(scene)'Unchecked_Access);
      this.Face(-this.Face);
      return True;
   end Try_Enter_Scene;

   function Try_Leave_Scene (
      this : in out Player_Type;
      target : Point_Type
      ) return Boolean
   is
   begin
      if this.F_Stage.Has_Exit_At(target) then
         Trace_Log("leave scene");
         this.Stop_Walk;
         this.F_Stage.Close;
         return True;
      end if;
      return False;
   end Try_Leave_Scene;

   overriding
   procedure On_Before_Walk (
      this : in out Player_Type; 
      Target : Point_Type;
      Allow : in out Boolean
      )
   is
   begin
      while Allow loop 
--         exit when Try_Leave_Scene(this, target);
         exit when Try_Enter_Scene(this, target);
         Role_Type(this).On_Before_Walk(target, allow);
         return;
      end loop;
      Allow := False;
   end On_Before_Walk;

   overriding
   procedure On_After_Walk (
      this : in out Player_Type
      )
   is
   begin
      this.F_Stage.On_Passby;
      if this.F_Stage.Has_Exit_At(this.Position) then
         Trace_Log("leave scene");
         this.Stop_Walk;
         this.F_Stage.Close;
      end if;
   end On_After_Walk;


end Legend.Roles.Specials;
